import fs from 'fs'

import { Bot as BotType } from 'mirai-js/dist/node/index'
import jsonfile from 'jsonfile'
const blackFile = ['index.ts', 'index.js', 'index.d.ts', 'index.js.map']
export const botModule = (bot: BotType): void => {
  let num = 0
  let helpObj = []
  fs.readdirSync(__dirname).forEach((fileName) => {
    if (!blackFile.includes(fileName)) {
      let file = require(`./${fileName}/index`)
      const fileHelp = jsonfile.readFileSync(`./src/modules/${fileName}/help.json`)
      helpObj.push(fileHelp)
      file.default(bot)
      num++
    }
  })
  // 将所有帮助文件统一写入静态文件
  jsonfile.writeFile('./src/static/help.static.json', helpObj, function (err) {
    if (err) console.error(err)
  })
  console.log(`加载了${num}个模块`)
}
